package com.starsea.book.ui.newpage.activity

import android.os.Bundle
import android.text.TextUtils
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import androidx.activity.viewModels
import androidx.core.widget.addTextChangedListener
import com.blankj.utilcode.util.ToastUtils
import com.starsea.book.base.VMBaseActivity
import com.starsea.book.databinding.ActivityRegisterBinding
import com.starsea.book.ui.book.info.BookInfoActivity
import com.starsea.book.ui.newpage.utils.AppManager
import com.starsea.book.ui.newpage.utils.visibleOrGone
import com.starsea.book.ui.newpage.viewmodel.LoginViewModel
import com.starsea.book.utils.isPhone
import com.starsea.book.utils.startActivity
import com.starsea.book.utils.viewbindingdelegate.viewBinding

/**
 * @author Lee
 * @createTime 2024-07-10 22 GMT+8
 * @desc :
 */
class RegisterActivity : VMBaseActivity<ActivityRegisterBinding, LoginViewModel>() {

    var loginType = 0
    override val viewModel: LoginViewModel by viewModels<LoginViewModel>()

    override val binding: ActivityRegisterBinding by viewBinding(ActivityRegisterBinding::inflate)

    override
    fun onActivityCreated(savedInstanceState: Bundle?) {

        AppManager.getInstance().addActivity(this)

        binding.titleBarView.setOnLeftTextClickListener {
            finish()
        }

        binding.etPhone.addTextChangedListener {
            val phone = it.toString()
            binding.ivDelete.visibleOrGone(!TextUtils.isEmpty(phone))
            verfiy()
        }

        binding.etPwd.addTextChangedListener {
            val phone = it.toString()
            binding.ivDeletePwd.visibleOrGone(!TextUtils.isEmpty(phone))
            verfiy()
        }

        binding.etPwdConfim.addTextChangedListener {
            val phone = it.toString()
            binding.ivDeletePwdConfim.visibleOrGone(!TextUtils.isEmpty(phone))
            verfiy()
        }

        binding.ivDelete.setOnClickListener {
            binding.etPhone.setText("")
        }

        binding.ivDeletePwd.setOnClickListener {
            binding.etPwd.setText("")
        }

        binding.ivDeletePwdConfim.setOnClickListener {
            binding.etPwdConfim.setText("")
        }

        binding.ivPwdShow.setOnClickListener {
            val isSel = binding.ivPwdShow.isSelected
            if (isSel) {
                binding.ivPwdShow.isSelected = false
                binding.etPwd.transformationMethod = PasswordTransformationMethod.getInstance()
            } else {
                binding.ivPwdShow.isSelected = true
                binding.etPwd.transformationMethod = HideReturnsTransformationMethod.getInstance()
            }
        }
        binding.ivPwdShowConfim.setOnClickListener {
            val isSel = binding.ivPwdShowConfim.isSelected
            if (isSel) {
                binding.ivPwdShowConfim.isSelected = false
                binding.etPwdConfim.transformationMethod =
                    PasswordTransformationMethod.getInstance()
            } else {
                binding.ivPwdShowConfim.isSelected = true
                binding.etPwdConfim.transformationMethod =
                    HideReturnsTransformationMethod.getInstance()
            }
        }


        binding.rtvLogin.setOnClickListener {
            val phone = binding.etPhone.text.toString()
            val pwd = binding.etPwd.text.toString()
            val newpwd = binding.etPwdConfim.text.toString()
            val recomendCode = binding.etReomendCode.text.toString()

            if (!phone.isPhone()) {
                ToastUtils.showShort("请输入正确的手机号")
                return@setOnClickListener
            }

            if (pwd.length < 6) {
                ToastUtils.showShort("请输入6-16位密码、数字或字母")
                return@setOnClickListener
            }
            if (newpwd.length < 6) {
                ToastUtils.showShort("请输入6-16位密码、数字或字母")
                return@setOnClickListener
            }
            if (pwd != newpwd) {
                ToastUtils.showShort("两次密码输入不一致")
                return@setOnClickListener
            }
            viewModel.getCode(phone)

        }

        viewModel.codeResult.observe(this) {
            when (it) {
                200 -> {
                    ToastUtils.showShort("短信发送成功")
                    val phone = binding.etPhone.text.toString()
                    val pwd = binding.etPwd.text.toString()
                    val recomendCode = binding.etReomendCode.text.toString()
                    startActivity(
                        CodeSignActivity::class,
                        "phone" to phone, "pwd" to pwd, "recomendCode" to recomendCode, "mType" to 2
                    )

                }

                else -> {
                }


            }
        }
    }


    private fun verfiy() {
        binding.rtvLogin.isEnabled = !TextUtils.isEmpty(binding.etPhone.text.toString())
                && binding.etPhone.text.toString().length == 11
                && !TextUtils.isEmpty(binding.etPwd.text.toString())
                && binding.etPwd.text.toString().length >= 6
                && !TextUtils.isEmpty(binding.etPwdConfim.text.toString())
                && binding.etPwdConfim.text.toString().length >= 6

    }


}